package com.example.myandroidactualcombat.ui.home;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.Adapter.BookAdapter;
import com.Adapter.CollectionAdapter;
import com.example.myandroidactualcombat.R;
import com.example.myandroidactualcombat.ReadActivity;
import com.example.myandroidactualcombat.data.Book;
import com.example.myandroidactualcombat.data.ChooseBook;
import com.example.myandroidactualcombat.data.Collection;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    List<Book>bookList;
    private ListView lv;
    CollectionAdapter collectionAdapter;
    List<Collection>collectionList;
    private HomeViewModel homeViewModel;
    int userid;
    String username;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        homeViewModel= ViewModelProviders.of(getActivity()).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        initView(root);
        addListener();
        addBook();
        getUser();
        homeViewModel.getData().observe(getViewLifecycleOwner(), new Observer<List<Collection>>() {
            @Override
            public void onChanged(List<Collection> collections) {
                collectionList=collections;
                collectionAdapter=new CollectionAdapter(getContext(),R.layout.collection,collectionList);
                Log.e("111", "onItemLongClick"+collectionList.size());
                lv.setAdapter(collectionAdapter);
            }
        });
        return root;
    }
    private void addListener() {
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                int bookid=bookList.get(i).getId();
                ChooseBook chooseBook = new ChooseBook(bookid, userid);
                List<ChooseBook>chooseBookList=LitePal.where("bookid=? and userid=?",bookid+"",userid+"").find(ChooseBook.class);
                if (chooseBookList.size()==0){
                    chooseBook.save();
                    Toast.makeText(getContext(), "成功收藏图书", Toast.LENGTH_SHORT).show();
                    for (int j=0;j<collectionList.size();j++){
                        if (bookid==collectionList.get(j).getBookid()){
                            collectionList.get(j).setIfcollection(true);
                        }
                    }
                    collectionAdapter.notifyDataSetChanged();
                }else{
                    Toast.makeText(getContext(), "图书已收藏！！！", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getContext(), ReadActivity.class);
                intent.putExtra("bookname",bookList.get(i).getBookname());
                startActivity(intent);
            }
        });
    }

    private void getUser() {
        //通过getDefaultSharedPreferences方法得到SharedPreferences的对象
        SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getContext());
        //分别调用getString,getint方法来获取前面储存的书名和序号
        username=pref.getString("username","无值");//这键值对的意思是，如果找不到相应的值就用后面的值替代
        userid=pref.getInt("userid",9);//假如userid没有值，将用9替代
    }

    private void addBook() {
        Book book0=new Book(0,"三体",R.drawable.z);//在litepal数据库里新增一条数据
        book0.save();//保存
        Book book1=new Book(1,"狼王梦",R.drawable.z);
        book1.save();
        Book book2=new Book(2,"第七条猎狗",R.drawable.z);
        book2.save();
        Book book3=new Book(3,"鸟奴",R.drawable.z);
        book3.save();

    }
    private void initView(View view) {
        lv = view.findViewById(R.id.lv_Dash);
        bookList=LitePal.findAll(Book.class);
    }
}